<?php

/**
 * alltosun.com store_helper.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 刘德洪 (Liudh@alltosun.com) $
 * $Date: $
 * $Id: store_helper.php 189434 2016-05-20 07:22:19Z wangdk $
 */
class store_helper
{
    /**
     * 根据总店id获取总店信息
     */
    public static function get_user_store_by_user_store_id($user_store_id) {
        return _uri('user_store', array('user_id' => $user_store_id));
    }

    /**
     * 分店是否绑定过设备
     * @param unknown_type $store_id
     */
    public static function is_user_store_bind_device($owner_id) {
        $store_list = store_helper::get_store_list_by_owner_id($owner_id);
        if ($store_list) {
            foreach ($store_list as $v) {
                if (self::is_store_bind_device($v['id'])) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 分店是否绑定过设备
     * @param unknown_type $store_id
     */
    public static function is_store_bind_device($store_id) {
        $filter['store_id'] = $store_id;
        $filter['status'] = 2;
        return _model('store_device_relation')->getTotal($filter);
    }

    /**
     * 获取经销商旗下的总店
     * @param unknown_type $user_store_id
     * @return Ambigous <multitype:, string, unknown, Obj, mixed>
     */
    public static function get_user_store_list_by_id($agency_id, $marketing_id) {
        $filter['status > '] = 0;
        if ($agency_id) {
            $filter['agency_id'] = $agency_id;
        }
        if ($marketing_id) {
            $filter['marketing_id'] = $marketing_id;
        }
        return _model('user_store')->getList($filter);
    }

    /**
     * 判断当前商家是否未调整过价格
     * @param unknown_type $user_store_id
     */
    public static function is_store_unset_price($user_store_id) {
        $price = $market_price = 0;
        $sku_price_list = _model('store_goods_sku')->getList(array('store_id' => $user_store_id));
        if ($sku_price_list) {

            foreach ($sku_price_list as $k => $v) {
                $price = $price + $v['price'];
                $market_price = $market_price + $v['market_price'];
            }

            return (string)$price == (string)$market_price;
        }

        return false;
    }

    /**
     * 更新店铺的商品的价格,跨合伙人迁移门店后，门店商品的价格需要重新初始化
     * @param unknown_type $owner_ids
     */
    public static function update_store_goods_price($owner_ids) {
        if ($owner_ids) {
            foreach ($owner_ids as $k => $owner_id) {
                $user_store_info = store_helper::get_user_store_info_by_id($owner_id);
                if ($user_store_info) {
                    $store_goods_list = store_helper::get_store_sku_list_by_user_store_id($user_store_info['id']);
                    // 重新初始改价的
                    if ($store_goods_list) {
                        store_helper::init_user_store_sku_price($store_goods_list, $user_store_info['id']);
                    }
                }
            }
        }
    }

    /**
     * 更新总店，分店为新的市场人员，事务操作
     * @param unknown_type $user_store_id
     * @param unknown_type $store_list
     * @param unknown_type $agency_id 跨合伙人迁移
     */
    public static function update_store_list_to_market($user_store_id, $store_list, $market_id, $agency_id = 0) {
        ts_helper::begin();
        try {

            if ($user_store_id) {

                $update_arr = array('marketing_id' => $market_id, 'sign_user_id' => $market_id);
                // 跨门店迁移
                if ($agency_id) {
                    $update_arr['agency_id'] = $agency_id;
                }
                ts_helper::_model('user_store')->update($user_store_id, $update_arr);
            }

            if ($store_list) {
                foreach ($store_list as $k => $v) {
                    $update_arr = array('marketing_id' => $market_id, 'sign_user_id' => $market_id);
                    if ($agency_id) {
                        $update_arr['agency_id'] = $agency_id;
                    }

                    ts_helper::_model('store')->update($v['id'], $update_arr);
                }
            }

            ts_helper::commit();
            return array(true);

        } catch (Exception $e) {
            ts_helper::trace_log($e);
            ts_helper::rollback();
        }

        return array(false, '门店迁移失败,请联系相关工作人员。');
    }

    /**
     * 根据$owner_id 获取分店信息
     * @param unknown_type $user_store_id
     * @return Ambigous <multitype:, string, unknown, Obj, mixed>
     */
    public static function get_user_store_list_id($owner_id) {
        return _model('store')->getList(array('owner_id' => $owner_id));
    }

    /**
     * 根据user_store_id 获取总店信息
     * @param unknown_type $user_store_id
     * @return Ambigous <multitype:, string, unknown, Obj, mixed>
     */
    public static function get_user_store_info_by_id($owner_id) {
        return _model('user_store')->read(array('user_id' => $owner_id));
    }

    /**
     * 通过总店id获取门店id列表
     * @param unknown $owner_id
     * @param unknown $filter
     * @return Ambigous <multitype:, NULL, mixed, multitype:Ambigous <multitype:, NULL> , array(), unknown>
     */
    public static function get_store_ids_by_owner_id($owner_id, $filter = array()) {
        $filter['owner_id'] = $owner_id;
        $filter['is_delete'] = 1;
        return _model('store')->getFields('id', enable_filter($filter));
    }

    /**
     * 通过合伙人id获取门店id列表
     * @param unknown $agency_id
     * @param unknown $filter
     * @return Ambigous <multitype:, NULL, mixed, multitype:Ambigous <multitype:, NULL> , array(), unknown>
     */
    public static function get_store_ids_by_agency_id($agency_id, $filter = array()) {
        $filter['agency_id'] = $agency_id;
        $filter['is_delete'] = 1;
        return _model('store')->getFields('id', $filter);
    }

    /**
     * 通过市场人员id获取门店id列表
     * @param unknown $marketing_id
     * @param unknown $filter
     * @return Ambigous <multitype:, NULL, mixed, multitype:Ambigous <multitype:, NULL> , array(), unknown>
     */
    public static function get_store_ids_by_marketing_id($marketing_id, $filter = array()) {
        $filter['marketing_id'] = $marketing_id;
        $filter['is_delete'] = 1;
        return _model('store')->getFields('id', $filter);
    }

    /**
     * 通过总店获取门店列表
     * @param unknown $owner_id
     */
    public static function get_store_list_by_owner_id($owner_id, $filter = array()) {
        $filter['owner_id'] = $owner_id;
        $filter['is_delete'] = 1;
        return _model('store')->getList($filter);
    }

    /**
     * 获取合伙人旗下门店列表
     * @param unknown_type $agency_id
     * @return unknown
     */
    public static function get_dealers_store_list($agency_id) {
        $store_list = array();
        $store_filter['agency_id'] = $agency_id;
        $store_filter['is_delete'] = 1;
        $store_filter['is_enable'] = 1;

        $store_list = _model('store')->getList($store_filter);
        return $store_list;
    }

    /**
     * 签约门店的时候，初始化合伙人的商品
     * @param unknown_type $user_id
     * @param unknown_type $store_id
     * @param unknown_type $user_store_id
     */
    public static function init_store_goods_price($user_id, $user_store_id) {
        // 把当前的商品列表初始化到改价列表
        // Fuck user_store 是 store表中的store_id,
        // store_goods_sku表中是  user_store 表中 store_id

        $sku_list = goods_helper::get_goods_all_sku();
        if ($sku_list) {
            foreach ($sku_list as $key => $val) {
                $store_info_sku_info = goods_helper::get_store_goods_sku_info(array(
                    'sku_id' => $val['sku_id'],
                    'store_id' => $user_store_id,
                    'goods_id' => $val['goods_id']
                ));

                // 看下商户库存里面有没有这个商品的sku信息，如果有的话增加库存并上架，如果没有插入一条并上架
                if (!$store_info_sku_info) {
                    goods_helper::create_store_goods_sku(array(
                        'user_id' => $user_id,
                        'goods_id' => $val['goods_id'],
                        'store_id' => $user_store_id,
                        'sku_id' => $val['sku_id'],
                        'price' => $val['market_price'],
                        'market_price' => $val['market_price'],
                        'stock' => 0,
                        'sale_count' => 0,
                        'status' => 1
                    ));
                }
            }
        }

    }

    /**
     * 获取经销商总店的商品库存
     * @param unknown_type $store_id
     * @return unknown
     */
    public static function get_agency_store_stock_list($store_id) {
        $sku_stock_list = array();
        $sku_list = _model('store_goods_sku')->getList(array('store_id' => $store_id));
        if ($sku_list) {
            foreach ($sku_list as $val) {
                $sku_stock_list[$val['goods_id']] = $val['stock'];
            }
        }

        return $sku_stock_list;
    }

    /**
     * 批量更新价格,跨合伙人迁移的时候要重新初始化价格
     * @param unknown_type $store_id
     * @param unknown_type $goods_id
     */
    public static function init_user_store_sku_price($store_goods_list, $user_store_id) {
        if ($store_goods_list) {
            $filter['store_id'] = $user_store_id;

            foreach ($store_goods_list as $key => $val) {
                $filter['goods_id'] = $val['goods_id'];

                $update_arr['price'] = $val['market_price'];
                _model('store_goods_sku')->update($filter, $update_arr);
            }
        }
    }

    /**
     * 获取经销商的sku库存列表库存
     * @param unknown_type $store_id
     * @param unknown_type $goods_id
     */
    public static function get_store_sku_list_by_user_store_id($user_store_id) {
        $filter['store_id'] = $user_store_id;
        return _model('store_goods_sku')->getList($filter);
    }

    /**
     * 获取经销商的sku库存列表库存
     * @param unknown_type $store_id
     * @param unknown_type $goods_id
     */
    public static function get_store_sku_info($store_id, $goods_id) {
        $filter['store_id'] = $store_id;
        $filter['goods_id'] = $goods_id;
        return _model('store_goods_sku')->getList($filter);
    }

    /**
     * 根据分店的ID获取总店的信息
     * @param unknown_type $store_id
     */
    public static function get_store_user_info($branch_store_id) {
        $store_info = store_helper::get_store($branch_store_id);
        if (!$store_info) {
            return false;
        }

        if (!$store_info['owner_id']) {
            return false;
        }

        return user_helper::get_user_info($store_info['owner_id']);
    }

    /**
     * 检查是否有店铺管理权限
     *
     * @param $user_info
     * @param $store_id
     * @return array
     */
    public static function check_store_auth($user_info, $store_id) {

        $user_id = $user_info['user_id'];
        $role_type = $user_info['role_type'];

        if (!$user_id) {
            return array(false, '请先登录');
        }

        if ($store_id <= 0) {
            return array(false, '商铺ID不能为空');
        }

        $store = store_helper::get_store($store_id);

        if (empty($store)) {
            return array(false, '商铺不存在');
        }

        if ($role_type == user_config::STORE_MANAGER) {
            //如果是分店店长身份
            if ($user_id != $store['user_id']) {
                return array(false, '无权访问');
            }
        } elseif ($role_type == user_config::STORE_OWNER) {
            //如果是经销商
            if ($user_id != $store['owner_id']) {
                return array(false, '无权访问');
            }
        } elseif ($role_type == user_config::AGENCY) {
            //如果是代理商
            if ($user_id != $store['agency_id']) {
                return array(false, '无权访问');
            }
        } elseif ($role_type == user_config::MARKETING) {
            //如果是市场人员
            if ($user_id != $store['marketing_id']) {
                return array(false, '无权访问');
            }
        } else {
            //api_helper::return_data(1, '身份不对');
        }

        return array(true, '');
    }

    /**
     * 获取店铺的详细信息
     * @param unknown_type $store_id
     * @return Ambigous <multitype:, string, unknown, Obj, mixed>
     */
    public static function get_store($store_id) {
        return $store = _uri('store', $store_id);
    }

    /**
     * 获取地店铺的标题
     * @param unknown_type $store_id
     * @param unknown_type $is_branch 是否返回分店名
     * @return string
     */
    public static function get_store_title($store_id, $is_branch = 0) {
        $store_info = self::get_store($store_id);
        if (!$store_info) {
            return false;
        }

        return empty($is_branch) ? $store_info['title'] : $store_info['title'] . $store_info['title_branch'];
    }

    /**
     * 获取店铺的图片
     * @param unknown_type $store_id
     * @return string
     */
    public static function get_store_cover($store_id) {
        $store = self::get_store($store_id);
        return @$store['cover'] . '';
    }

    /**
     * 获取当前用户的店铺信息
     * @param unknown_type $owner_id
     * @return Ambigous <multitype:, string, unknown, Obj, mixed>
     */
    public static function get_user_store($owner_id) {
        return _uri('user_store', array(
            'user_id' => $owner_id,
        ));
    }

    /**
     * 获取经销商下的总店数
     */
    public static function get_user_store_num($role_type, $user_id) {
        $filter = array(
            'status' => array(
                store_config::STATUS_NORMAL,
                store_config::STATUS_TRIAL,
            ),
        );

        if ($role_type == user_config::AGENCY) {
            $filter['agency_id'] = $user_id;
        } else {
            if ($role_type == user_config::MARKETING) {
                $filter['marketing_id'] = $user_id;
            }
        }

        $num = _model('user_store')->getTotal($filter);
        return $num + 0;
    }

    /**
     * 获取 总店老板的所有分店
     * @param $uid
     */
    public static function get_owner_stores($owner_id) {
        return _model('store')->getList(array(
            'owner_id' => $owner_id,
            'status !=' => '0',
        ));
    }

    //获取分店的经销商店铺
    public static function get_agency_store($store_id) {
        $store_info = _uri('store', $store_id);
        if (!$store_info) {
            return array();
        }
        $agency_store_info = _uri('store', array('user_id' => $store_info['agency_id']));
        return $agency_store_info;
    }

    /**
     * 获取总店信息
     * @param unknown $owner_id
     */
    public static function get_user_store_info($owner_id) {
        if (!$owner_id) {
            return array();
        }

        return _uri('user_store', array('user_id' => $owner_id));

    }

    /**
     * 获取总店下面分店的数量
     */
    public static function get_owner_stores_count($owner_id) {
        return _model('store')->getTotal(array(
            'owner_id' => $owner_id,
            'status >' => '0',
        ));
    }

    /**
     * 获取经销商下的店铺列表
     */
    public static function get_agency_stores($agency_id) {
        return _model('store')->getList(array(
            'agency_id' => $agency_id,
            'status >' => '0',
        ));
    }

    /**
     * 获取市场人员的店铺列表
     */
    public static function get_marketing_stores($marketing_id) {
        return _model('store')->getList(array(
            'marketing_id' => $marketing_id,
            'status >' => '0',
        ));
    }

    /**
     * 获取商户的详细信息，包括市场人员和经销商的详细信息
     * @param unknown_type $store_id
     * @return boolean|Ambigous <multitype:, string, unknown, Obj, mixed>
     */
    public static function get_store_sub_info($store_id) {
        // 获取门店信息
        $store_info = self::get_store($store_id);

        if (!$store_info) {
            return false;
        }

        // 获取门店店长的用户信息
        $store_user_info = user_helper::format_user_info($store_info['user_id']);
        //获取门店市场人员的用户信息
        $marketing_user_info = user_helper::format_user_info($store_info['marketing_id']);
        if (empty($marketing_user_info)) {
            // TODO 如果市场人员不存在, 可能是被删了. 取合伙人本人
            $marketing_user_info = user_helper::format_user_info($store_info['agency_id']);
        }

        $store_info['store_user_info'] = $store_user_info;
        $store_info['marketing_user_info'] = $marketing_user_info;

        return $store_info;
    }

    /**
     * 获取商户的详细信息，包括市场人员和经销商的详细信息
     * @param unknown_type $store_id
     * @return boolean|Ambigous <multitype:, string, unknown, Obj, mixed>
     */
    public static function get_store_info($store_id) {
        // 获取门店信息
        $store_info = self::get_store($store_id);
        if (!$store_info) {
            return false;
        }

        // 获取门店店长的用户信息
        $store_user_info = user_helper::get_user_info($store_info['user_id']);
        //获取门店市场人员的用户信息
        $marketing_user_info = user_helper::get_user_info($store_info['marketing_id']);

        $store_info['store_user_info'] = $store_user_info;
        $store_info['marketing_user_info'] = $marketing_user_info;

        return $store_info;
    }

    /**
     * 根据用户ID获取店铺详情
     * @param unknown_type $user_id
     * @return multitype:|Ambigous <multitype:, string, unknown, Obj, mixed>
     */
    public static function get_store_info_by_user_id($user_id) {
        if (!$user_id) {
            return array();
        }
        $store_info = _uri('store', array('user_id' => $user_id));
        return $store_info;
    }

    public static function cover($image) {
        if (!$image) {
            return STATIC_URL . '/images/movie_theater.jpg';
        } else {
            return _image($image);
        }
    }

    /**
     * 通过店铺id获取店主用户id
     */
    public static function get_user_id_by_store_id($store_id) {
        if (!$store_id) {
            return false;
        }

        $store_info = _uri('store', $store_id);
        if ($store_info) {
            return $store_info['user_id'];
        } else {
            return 0;
        }

    }

    /**
     * 通过店铺id获取合伙人id
     */
    public static function get_agency_id_by_store_id($store_id) {
        if (!$store_id) {
            return false;
        }

        $store_info = _uri('store', $store_id);
        if ($store_info) {
            return $store_info['agency_id'];
        } else {
            return 0;
        }

    }

    /**
     * 判断店铺是新旧经销商
     */
    public static function is_old($store_id) {
        if (!$store_id) {
            return false;
        }

        $store_info = self::get_store($store_id);
        if ($store_info) {
            return $store_info['is_old'];
        }

        return false;
    }

    /**
     * 更新店铺初始化库存
     * @param  [type] $store_id [description]
     * @return [type]           [description]
     */
    public static function update_init_store($store_id) {
        if (!$store_id) {
            return false;
        }
        return _model('store')->update($store_id, array('is_init' => 1));
    }

    /**
     * 通过手机号获取店铺id
     * @param $mobile : 手机号
     * @param $is_owner : 是否是总店 1:总店 0:分店
     * @param $store_order : 如果传递了$is_owner是0,则需要传递需要获取第几家分店 默认是第一家分店
     */
    public static function get_store_id_by_mobile($mobile, $is_owner = 1, $store_order = 1) {
        if (!$mobile) {
            return false;
        }

        $user_ids = _model('user')->getFields('id', array('mobile' => $mobile), 'ORDER BY `id`');

        if (!$user_ids) {
            return false;
        }

        if (1 == $is_owner) {
            $owner_id = $user_ids[0];
            $store_info = _uri('store', array('owner_id' => $owner_id));
            if ($store_info) {
                return $store_info['id'];
            } else {
                return 0;
            }
        } else {
            if ($store_order < 1) {
                return false;
            }
            if (!isset($user_ids[$store_order])) {
                return false;
            }
            $store_user_id = $user_ids[$store_order];
            $store_info = _uri('store', array('user_id' => $store_user_id));
            if ($store_info) {
                return $store_info['id'];
            } else {
                return 0;
            }

        }
    }


    /**
     * 查看门店的绑定设备列表
     */
    public static function get_store_device_list($store_id) {
        if (!$store_id) {
            return false;
        }

        $device_list = _model('store_device_relation')->getList(array('store_id' => $store_id, 'status' => 2));

        $store_device_list = array();
        $store_device_list['old_machine'] = array('status' => '', 'device_no' => '');
        $store_device_list['machine'] = array('status' => '', 'device_no' => '');
        $store_device_list['adbox'] = array('status' => '', 'device_no' => '');
        $store_device_list['ibeacon'] = array('status' => '', 'device_no' => '');
        $store_device_list['game_table'] = array('status' => '', 'device_no' => '');
        $store_device_list['device_list'] = array(
            'old_machine' => array(),
            'machine' => array(),
            'adbox' => array(),
            'ibeacon' => array(),
            'game_table' => array(),

        );
        if ($device_list) {
            foreach ($device_list as $k => $v) {
                $device_id = $v['res_id'];
                $res_name = $v['res_name'];
                $device_info = _uri($res_name, $device_id);
                if ($device_info) {
                    $device_no = $device_info[$res_name . '_no'];
                    $status = $device_info['status'] . '';

                    $pre_sign = '';
                    if ('machine' == $res_name) {
                        if (machine_helper::is_old_machine($device_no)) {
                            $pre_sign = 'old_';
                        }
                    }
                    $store_device_list[$pre_sign . $res_name]['status'] = $status;
                    $store_device_list[$pre_sign . $res_name]['device_no'] = $device_no;

                    $store_device_info = array();
                    $store_device_info['status'] = $status;
                    $store_device_info['device_no'] = $device_no;
                    array_push($store_device_list['device_list'][$pre_sign . $res_name], $store_device_info);
                }
            }
            return $store_device_list;
        } else {
            return $store_device_list;
        }

    }

    /**
     * 获取总店下分店的设备绑定数量
     */
    public static function get_store_device_num_list($store_id) {

        if (!$store_id) {
            return false;
        }

        $store_info = store_helper::get_store_info($store_id);
        if ($store_info) {
            $store_list = _model('store')->getList(array(
                'owner_id' => $store_info['owner_id'],
                'status !=' => store_config::STATUS_STOP
            ));
        } else {
            return false;
        }

        $store_device_num_list = array();
        if ($store_list) {
            foreach ($store_list as $k => $v) {
                $store_device_num_list[$k]['id'] = $v['id'];
                $device_num = _model('store_device_relation')->getTotal(array('store_id' => $v['id'], 'status' => 2));
                $store_device_num_list[$k]['device_num'] = $device_num;
                if ($device_num) {
                    $store_device_num_list[$k]['device_num_str'] = $device_num . '个设备';
                } else {
                    $store_device_num_list[$k]['device_num_str'] = '暂无';
                }

                $store_device_num_list[$k]['store_title'] = $v['title'] . '（' . $v['title_branch'] . '）';

            }
        }

        return $store_device_num_list;

    }

    /**
     * 查看门店的设备绑定记录
     */
    public static function get_store_device_record($store_id) {
        if (!$store_id) {
            return false;
        }

        $store_info = _uri('store', $store_id);

        $record_list = array();
        $record_list = _model('store_device_record')->getList(array('store_id' => $store_id));
        if ($record_list) {
            foreach ($record_list as $k => $v) {
                $res_name = $v['res_name'];
                $device_info = _uri($res_name, $v['res_id']);
                $record_list[$k]['device_no'] = $device_info[$res_name . '_no'];
                if ('adbox' == $res_name) {
                    $record_list[$k]['device_name'] = '广告机';
                } else {
                    if (machine_helper::is_old_machine($device_info[$res_name . '_no'])) {
                        $record_list[$k]['device_name'] = '一代保温箱';
                    } else {
                        $record_list[$k]['device_name'] = '二代保温箱';
                    }
                }
            }
        }

        return $record_list;

    }

    /**
     * 获取所有的有效分店数
     */
    public static function get_all_store_num($filter = array()) {
        $filter['is_delete'] = 1;
        $filter['is_enable'] = 1;
        $filter['`agency_id` != '] = 0;
        return _model('store')->getTotal($filter);
    }

    public static function get_marketing_user_store_num($marketing_id) {
        if (!$marketing_id) {
            return 0;
        }

        return _model('user_store')->getTotal(array('marketing_id' => $marketing_id, '`status` != ' => 0));
    }

    /**
     * 获取总店下指定状态的分店数量
     */
    public static function get_store_num($owner_id, $status = -1) {
        if (!$owner_id) {
            return false;
        }

        $filter = array();
        $filter['owner_id'] = $owner_id;
        if ($status != -1) {
            $filter['status'] = $status;
        }

        return _model('store')->getTotal($filter);
    }

    /**
     * 解除店铺id对应的所有设备
     */
    public static function unbind_store_device($store_id) {
        if (!$store_id) {
            return false;
        }

        $relation_list = _model('store_device_relation')->getList(array('store_id' => $store_id));

        if ($relation_list) {
            foreach ($relation_list as $k => $v) {
                $res_name = $v['res_name'];
                $device_id = $v['res_id'];
                if ('adbox' == $res_name || 'machine' == $res_name) {
                    $device_info = _uri($res_name, $device_id);
                    $device_no = $device_info[$res_name . '_no'];
                    _widget('machine')->bind_store_device($store_id, $device_no, 1, '停业撤店', '');
                } else {
                    continue;
                }
            }
        }
        return true;
    }

    /**
     * 获取门店商品信息
     */
    public static function get_store_goods_info($filter) {
        if (!$filter || !is_array($filter)) {
            return array();
        }

        return _uri('store_goods', $filter);

    }

    /**
     * 添加门店商品
     */
    public static function create_store_goods($user_id, $store_id, $goods_id, $status) {
        if (!$user_id || !$store_id || !$goods_id) {
            return 0;
        }

        return _model('store_goods')->create(array(
            'user_id' => $user_id,
            'goods_id' => $goods_id,
            'store_id' => $store_id,
            'status' => $status
        ));
    }
    //
    /**
     * 统计指定的经销商(们)下所有的门店列表
     * @wangdk 如果总店被删除，注意分店
     */
    public static function get_store_list_by_dealer_id($dealer_id, $filter = array()) {
        if (!$dealer_id) {
            return array();
        }

        $filter['agency_id'] = $dealer_id;
        $filter['status'] = array(1, 2, 3);
        $filter['is_delete'] = 1;
        return _model('store')->getList($filter);
    }

    /**
     * 统计指定的经销商(们)下所有的门店列表
     * @wangdk 如果总店被删除，注意分店
     */
    public static function get_store_agency_ids_by_update_time($filter = array()) {
        $filter['status'] = array(1, 2, 3);
        $filter['is_delete'] = 1;
        $half_sql = " GROUP BY `agency_id`  ";
        return _model('store')->getFields('agency_id', $filter, $half_sql);
    }

    /**
     * 统计门店的进货额度
     */
    public static function get_store_stock_money($store_id, $order_progress_filter = array(5, 10)) {
        if (!$store_id) {
            return 0;
        }
        $filter = array(
            'progress' => $order_progress_filter,
            'type' => 5,
            'is_enable' => 1,
            'status' => 1
        );

        $store_order_list = order_helper::get_store_order_list($store_id, $filter);
        $money = 0;
        if ($store_order_list) {
            foreach ($store_order_list as $k => $v) {
                $money += $v['price'];
            }
        }
        return $money;
    }

    /**
     * 统计门店的进货数量
     */
    public static function get_store_stock_num($store_id, $order_progress_filter = array(5, 10)) {
        if (!$store_id) {
            return 0;
        }
        $filter = array(
            'progress' => $order_progress_filter,
            'type' => 5,
            'is_enable' => 1,
            'status' => 1
        );

        $store_order_list = order_helper::get_store_order_list($store_id, $filter);
        $number = 0;
        if ($store_order_list) {
            foreach ($store_order_list as $k => $v) {
                $number += $v['goods_num'];
            }
        }
        return $number;
    }

    /**
     * 统计门店的出货数量
     */
    public static function get_store_sell_num($store_id, $order_progress_filter = array(5, 10)) {
        if (!$store_id) {
            return 0;
        }
        $filter = array(
            'progress' => $order_progress_filter,
            'type' => 1,
            'is_enable' => 1,
            'status' => 1
        );

        $store_order_list = order_helper::get_store_order_list($store_id, $filter);
        $number = 0;
        if ($store_order_list) {
            foreach ($store_order_list as $k => $v) {
                $number += $v['goods_num'];
            }
        }
        return $number;
    }

    /**
     * 更新经销商的默认仓库地址
     */
    public static function update_dealer_default_address($dealer_address_id, $order_id) {
        if (!$dealer_address_id) {
            return false;
        }

        $dealer_address_info = _uri('dealer_address', $dealer_address_id);
        if (!$dealer_address_info) {
            return false;
        }
        // 更新仓库地址关系表
        $dealer_address_list = _model('dealer_address')->getList(array('user_id' => $dealer_address_info['user_id']));
        foreach ($dealer_address_list as $k => $v) {
            if ($v['id'] == $dealer_address_id) {
                _model('dealer_address')->update($dealer_address_id, array('is_default' => 1));
            } else {
                _model('dealer_address')->update($v['id'], array('is_default' => 0));
            }
        }

        // 更新用户的默认地址
        if ('000000' != $dealer_address_info['county_code']) {
            $area_code = $dealer_address_info['county_code'];
        } elseif ('000000' != $dealer_address_info['city_code']) {
            $area_code = $dealer_address_info['city_code'];
        } elseif ('000000' != $dealer_address_info['province_code']) {
            $area_code = $dealer_address_info['province_code'];
        } else {
            $area_code = '000000';
        }

        // TODO 准备剥离独立方法
        // 更新订单的收货地址,收货人和收货人电话
        _model('order')->update($order_id, array(
            // 'dealer_address_id' => $dealer_address_id,
            'province_code' => $dealer_address_info['province_code'],
            'city_code' => $dealer_address_info['city_code'],
            'county_code' => $dealer_address_info['county_code'],
            'dealer_address' => $dealer_address_info['dealer_address'],
            'receiver' => $dealer_address_info['dealer_name'],
            'mobile' => $dealer_address_info['dealer_mobile']
        ));

        return _model('user')->update(array('id' => $dealer_address_info['user_id']), array(
            'area_code' => $area_code,
            'store_house' => $dealer_address_info['dealer_address']
        ));

    }

    /**
     * 获取经销商默认仓库地址id
     */
    public static function get_dealer_address_id($user_id) {
        $dealer_address_info = self::get_dealer_address_info($user_id);
        if ($dealer_address_info) {
            return $dealer_address_info['id'];
        }

        return 0;
    }

    /**
     * 获取经销商默认仓库信息
     */
    public static function get_dealer_address_info($user_id) {
        if (!$user_id) {
            return array();
        }
        return _uri('dealer_address', array('user_id' => $user_id, 'is_default' => 1, 'status' => 1));
    }

    /**
     * 根据区域获取下面的店
     * 有问题,废弃
     */
    public static function get_store_ids_by_area($province_id, $city_id = array(), $district_id = array(), $types = array()) {
        $sql = array();
        if ($province_id) {
            $sql['province_id'] = $province_id;
        }
        if ($city_id) {
            $sql['city_id'] = $city_id;
        }
        if ($district_id) {
            $sql['district_id'] = $district_id;
        }
        if ($types) {
            $sql['type'] = $types;
        }
        return _model('store')->getFields('id', $sql);
    }


    public static function get_store_ids_by_admin_user($store_type = array()) {
        //获取当前用户可看的区域
        $role_id = user_helper::get_admin_role_id();
        if (!user_helper::is_all_can_show($role_id)) {
            $admin_id = user_helper::get_admin_id();
            $province_codes = _model('multiple_area')->getFields('province_code', array(
                'user_id' => $admin_id,
                'status' => 1
            ));

            if ($province_codes) {
                $address_dealer_ids = _model('dealer_address')->getFields('user_id', array('province_code' => $province_codes));
                if ($address_dealer_ids) {
                    $filter = array(
                        'is_enable' => 1,
                        'is_delete' => 1,
                        'agency_id' => $address_dealer_ids,
                        'type' => $store_type
                    );

                    $store_ids = _model('store')->getFields('id', enable_filter($filter));
                } else {
                    return false;
                }
            }
        } else {
            $filter = array(
                'is_enable' => 1,
                'is_delete' => 1,
                'type' => $store_type
            );

            $store_ids = _model('store')->getFields('id', enable_filter($filter));
        }

        return $store_ids;
    }

    /**
     * 根据店铺的id集合,获取这些店铺近半年的进货金额及订单数量
     *
     * @author xiaolin
     * @date   2016-10-18
     * @param array $store_ids 店铺的id集合
     * @return array
     * @throws AnException
     */
    public static function get_half_year_order_by_store_id($store_ids) {
        $data = array();
        $half_year_list = self::recent_half_year();

        $filter['store_id'] = $store_ids;
        $filter['status'] = 1;
        $filter['progress !='] = 15;
        $filter['is_enable'] = 1;
        foreach ($half_year_list as $k => $v) {
            $filter['receiver_time >='] = $v[0];
            $filter['receiver_time <='] = $v[1];
            $amount = _model('order')->getFields('price', enable_filter($filter));
            $count = count($amount);
            $amount = $amount ? format_money(array_sum($amount)) : "0.00";
            $data[] = array('month' => $k, 'price_sum' => $amount, 'order_sum' => (string)$count);
        }
        return $data;
    }

    /**
     * 获取当前日期之前半年里每个月的第一天和最后一天
     *
     * @author xiaolin
     * @date   2016-10-18
     * @param date $givenMonth
     * @return array
     */
    private static function recent_half_year($givenMonth = null) {
        $now = date('Y-m-d');
        if ($givenMonth) {
            $now = $givenMonth;
        }
        $month_list = array();
        for ($i = 5; $i >= 0; $i--) {
            $oldday = date('Y-m-d', strtotime("$now - $i month"));
            $oldmonth = date('Y-m', strtotime("$now - $i month"));
            $month_list[$oldmonth] = self::getTheMonth($oldday);
        }
        return $month_list;
    }

    /**
     * 获取指定日期所在月的第一天和最后一天
     *
     * @author xiaolin
     * @date   2016-10-18
     * @param date $date
     * @return array
     */
    private static function getTheMonth($date) {
        $firstday = date("Y-m-01 00:00:00", strtotime($date));
        $lastday = date("Y-m-d 23:59:59", strtotime("$firstday +1 month -1 day"));
        return array($firstday, $lastday);
    }
}